package com.nanosheep.freewheel;

import org.andnav.osm.ResourceProxy;
import org.andnav.osm.events.MapListener;
import org.andnav.osm.events.ScrollEvent;
import org.andnav.osm.events.ZoomEvent;
import org.andnav.osm.views.OpenStreetMapView;
import org.andnav.osm.views.overlay.MyLocationOverlay;
import org.andnav.osm.views.util.OpenStreetMapRendererFactory;
import org.andnav.osm.util.GeoPoint;

import com.nanosheep.accessmap.R;
import com.nanosheep.freewheel.overlay.LiveMarkers;

import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class AccessMap extends OpenStreetMapActivity implements MapListener{
	
	private static final int ZOOM = 15;
	/** Stand markers overlay. */
	private LiveMarkers stands;
	private ResourceProxy mResourceProxy;
	protected LocationManager mLocationManager;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        
        //Initialize map
        this.mOsmv = (OpenStreetMapView) findViewById(R.id.mapview);
        this.mOsmv.setResourceProxy(mResourceProxy);
        mOsmv.setRenderer(OpenStreetMapRendererFactory.CYCLEMAP);
        this.mLocationOverlay = new MyLocationOverlay(this.getBaseContext(), this.mOsmv);
        this.mOsmv.setBuiltInZoomControls(true);
        this.mOsmv.setMultiTouchControls(true);
        this.mOsmv.getOverlays().add(this.mLocationOverlay);
        
        mOsmv.getController().setZoom(mPrefs.getInt(getString(R.string.prefs_zoomlevel), 1));
        mOsmv.scrollTo(mPrefs.getInt(getString(R.string.prefs_scrollx), 0), 
        		mPrefs.getInt(getString(R.string.prefs_scrolly), 0));
		mOsmv.setBuiltInZoomControls(true);
		
		mOsmv.getController().setZoom(ZOOM);
		
		//Update nodes. //
		stands = new LiveMarkers(mOsmv, this);
		stands.refresh(mOsmv.getMapCenter());
		this.mOsmv.setMapListener(this);
    }
    
    @Override
	public void onStart() {
		super.onStart();
		this.mLocationOverlay.enableMyLocation();
        this.mLocationOverlay.followLocation(true);
	}
	
	@Override
	public void onStop() {
		super.onStop();
		this.mLocationOverlay.disableMyLocation();
	}

	/* (non-Javadoc)
	 * @see org.andnav.osm.events.MapListener#onScroll(org.andnav.osm.events.ScrollEvent)
	 */
	@Override
	public boolean onScroll(ScrollEvent arg0) {
		stands.refresh(mOsmv.getMapCenter());
		return true;
	}

	/* (non-Javadoc)
	 * @see org.andnav.osm.events.MapListener#onZoom(org.andnav.osm.events.ZoomEvent)
	 */
	@Override
	public boolean onZoom(ZoomEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}
	
	/**
	 * Create the options menu.
	 * @return true if menu created.
	 */

	@Override
	public final boolean onCreateOptionsMenu(final Menu menu) {
		final MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mapmenu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		mLocationOverlay.runOnFirstFix(new Runnable() {
			@Override
			public void run() {
				Location self = mLocationOverlay.getLastFix();
				
				if (self == null) {
					self = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
				}
				if (self == null) {
					self = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
				}
				if (self != null) {
					AccessMap.this.mOsmv.getController().animateTo(
						new GeoPoint(self.getLatitude(), self.getLongitude()));
				}
			}
		});
		return true;
	}
}